﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using UtilZ.Dotnet.Ex.Model;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// Object扩展方法类
    /// </summary>
    public static class ObjectEx
    {


        /// <summary>
        /// 抛出指定对象释放异常
        /// </summary>
        /// <param name="obj">目标对象</param>
        /// <exception cref="ArgumentNullException">目标对象为null将抛出空引用异常</exception>
        /// <exception cref="ObjectDisposedException">目标对象释放异常</exception>
        public static void ThrowObjectDisposedException(this object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            else
            {
                throw new ObjectDisposedException(obj.ToString());
            }
        }

        /// <summary>
        /// 抛出指定对象释放异常
        /// </summary>
        /// <param name="obj">目标对象</param>
        /// <exception cref="ArgumentNullException">目标对象为null将抛出空引用异常</exception>
        /// <exception cref="ObjectDisposedException">目标对象释放异常</exception>
        public static void ThrowObjectDisposedException(this IDisposableZ obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            else
            {
                if (obj.Disposabled)
                {
                    throw new ObjectDisposedException(obj.ToString());
                }
            }
        }

    }


    /// <summary>
    /// IDisposable扩展接口
    /// </summary>
    public interface IDisposableZ : IDisposable
    {
        /// <summary>
        /// 获取当前对象是否已释放[true:已释放;false:未释放]
        /// </summary>
        bool Disposabled { get; }
    }
}
